import pymysql,requests,urllib3,datetime,re
from lxml import etree
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)

host = '127.0.0.1'
users = 'root'
pwd = '123123'
port = 3306
dbs = 'ans'


def mysql(question,ans,answ):
    value = ((question,ans,answ))
    db = pymysql.connect(host=host, user=users,passwd=pwd,port=port, db=dbs)
    cursor = db.cursor()
    sql = "INSERT INTO qingshu(question,ans,answ) values(%s,%s,%s)"
    try:
        cursor.execute(sql, value)
        db.commit()
        print('success!')
    except Exception as e:
        db.rollback()
        print("error.", e)
    db.close()

url = ['https://degree.qingshuxuetang.com/hngd/Student/ViewExerciseAnswer?courseId=1880&exerciseId=620727e1cff47e0001b04f52&teachPlanId=370&periodId=17',
       'https://degree.qingshuxuetang.com/hngd/Student/ViewExerciseAnswer?courseId=1880&exerciseId=620727ebcff47e000103bd5c&teachPlanId=370&periodId=17',
       'https://degree.qingshuxuetang.com/hngd/Student/ViewExerciseAnswer?courseId=1880&exerciseId=620727f4cff47e000103bd80&teachPlanId=370&periodId=17',
       ]
for i in url:
    headers = {'cookie': 'AccessToken=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1SWQiOjI4NTg4NzY5LCJyb2xlIjoxMDAsImNsaWVudCI6InBjd2ViIiwib3JncyI6WyJ7XCJ0eXBlXCI6XCJkZ1wiLFwicm9sZXNcIjpbMV0sXCJpZFwiOjksXCJvdUlkXCI6MzAxNTI0fSJdLCJleHAiOjE2NjM4NTIwNTYsImp0aSI6Imp3dDYxYTBiMGM5NWZiYzRiN2JhMGY0Njc1MGZiODg4MDg2IiwicGxhdGZvcm0iOiJxc3h0In0.fCBk7WF8-dKdAqPD5e2douiNxKCYpwEz20Cl5hijkjY; __environment=production','referer': 'https://degree.qingshuxuetang.com/hngd/Student/Course/CourseStudy?courseId=1020&teachPlanId=370&periodId=17&tabType=homework','user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.5005.124 Safari/537.36 Edg/102.0.1245.44',}
    HTML = requests.get(url=i, headers=headers).text
    tree = etree.HTML(HTML)
    div_list = tree.xpath('//*[@id="form1"]/div')
    for div in div_list:
        question = div.xpath('./div[@class="test-heading"]/h4//text()')
        question = ''.join(question).replace('\n','').replace('             ','').replace('          ','')[3:]
        # num = question[0]
        ans = ''.join(div.xpath('./div[4]/span[2]//text()'))
        if ans == 'A':
            answ = ''.join(div.xpath('./div[2]/label[1]//text()')).replace('\n','').replace(' ','').replace('                            ','').replace(' ','')[2:]
        elif ans =='B':
            answ = ''.join(div.xpath('./div[2]/label[2]//text()')).replace('\n','').replace(' ','').replace('                            ','').replace(' ','')[2:]
        elif ans =='C':
            answ = ''.join(div.xpath('./div[2]/label[3]//text()')).replace('\n','').replace(' ','').replace('                            ','').replace(' ','')[2:]
        else:
            answ = ''.join(div.xpath('./div[2]/label[4]//text()')).replace('\n','').replace(' ','').replace('                            ','').replace(' ','')[2:]
        print(question,answ)
        mysql(question,ans,answ)